package base.http.interceptor;


import android.text.TextUtils;


import com.blankj.utilcode.util.SPUtils;
import com.tange.ai.iot.client.gnssa.App;
import com.tange.ai.iot.client.gnssa.token.InternalAccountLoginActivity;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator_LZH on 2016/6/23.
 */
public class ParamsInterceptor implements Interceptor {

    private static final ParamsInterceptor ourInstance = new ParamsInterceptor();

    public static ParamsInterceptor getInstance() {
        return ourInstance;
    }

    private ParamsInterceptor() {
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Map<String, Object> refreshParamsMap = new HashMap<>();
//        if (!TextUtils.isEmpty(SharedPreferencesUtils.getString(Const.SP.LOGIN_DATA, Const.SP.TOKEN))) {
//            refreshParamsMap.put("accessToken", SharedPreferencesUtils.getString(Const.SP.LOGIN_DATA, Const.SP.TOKEN));
//        } else {
//            refreshParamsMap.put("accessToken", "");
//        }
//        if(!TextUtils.isEmpty(HttpSp.getCookie())){
//            try {
//                HashMap map = GsonUtils.fromJson(HttpSp.getCookie(), new TypeToken<HashMap<String, String>>() {
//                }.getType());
//                refreshParamsMap.putAll(map);
//            }catch (Exception e) {
//            }
//        }loginWay

//        if (TextUtils.isEmpty(App.Companion.getToken())) {
//            refreshParamsMap.put("Authorization", "Basic aW90LXNhYXM6MTIzNDU2");
//        }else{
//            refreshParamsMap.put("Authorization", App.Companion.getToken());
//        }
//
//        if(!TextUtils.isEmpty(SPUtils.getInstance().getString("smsCode"))){
//            refreshParamsMap.put("smsCode",SPUtils.getInstance().getString("smsCode"));
//        }
//        if(!TextUtils.isEmpty(SPUtils.getInstance().getString("countryCode"))){
//            refreshParamsMap.put("countryCode",SPUtils.getInstance().getString("countryCode"));
//        }

        refreshParamsMap.put("Authorization", "Bearer " + SPUtils.getInstance().getString(InternalAccountLoginActivity.PRE_TOKEN));

//        refreshParamsMap.put("Host", Const.BASE);

        Request original = chain.request();
        Request.Builder builder = original.newBuilder();
        for (Map.Entry<String, Object> entry : refreshParamsMap.entrySet()) {
            builder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
        }
        builder.method(original.method(), original.body());
        return chain.proceed(builder.build());
    }

}
